from datetime import datetime

from django.shortcuts import render
from django.views import View
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.mixins import CreateModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from user.models import User, Clazz
from user.serializers import UserSerializer, ClazzSerializer


# Create your views here.
class UserView(CreateModelMixin,GenericViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes =[AllowAny]
    def create(self, request, *args, **kwargs):
        #判断帐号是否已被注册
        queryset =  self.get_queryset().filter(username=request.data['username'])
        print(queryset,"====================================================")
        if queryset:
            #提示帐号已被注册
            return Response(data ={"message": "该账号已被注册"},status=status.HTTP_404_NOT_FOUND)
        return super().create(request,*args,**kwargs)
class ClazzView(ModelViewSet):
    queryset = Clazz.objects.all()
    serializer_class = ClazzSerializer
    permission_classes = [AllowAny]
    #查询所有可用的班级
    @action(methods=['GET'],detail=False,url_path="can_user_list",name="can_user_list")
    def can_use_clazz(self, request, *args, **kwargs):
        #查询没有毕业的班级，没有删除的班级
        queryset = self.get_queryset().filter(is_delete=False,state =False)
        #序列化满足条件的数据
        serializer = self.get_serializer(queryset,many=True)
        return Response(data = serializer.data,status=status.HTTP_200_OK)
class ClassViewSet(ModelViewSet):
    queryset = Clazz.objects.all()
    serializer_class = ClazzSerializer
    permission_classes = [AllowAny]
    pagination_class = PageNumberPagination
    PageNumberPagination.page_size_query_param = "rows"
    def list(self, request, *args, **kwargs):
        #查询所有可用的班级列表
        queryset = self.get_queryset().filter(is_delete=False).order_by('state')
        #进行分页
        paginate = self.paginate_queryset(queryset)
        if paginate is None:
            return Response(data={"message":"该请求必须进行分页操作"} ,status=status.HTTP_400_BAD_REQUEST)
        #对分页后的结果进行序列化
        serializer = self.get_serializer(paginate, many=True)
        #返回分页后的结果
        return self.get_paginated_response(serializer.data)
    def create(self, request, *args, **kwargs):
        #查询班级名
        queryset = self.get_queryset().filter(name=request.data['name'],is_delete=False)
        if queryset:
            return Response(data={"message":"班级名已存在"},status=status.HTTP_400_BAD_REQUEST)
        return super().create(request,*args,**kwargs,state = True)
    @action(methods=["DELETE"],detail = False,url_path="batch-remove",name="batch_remove")
    def batch_remove(self, request, *args, **kwargs):
        # 逻辑删除
        self.get_queryset().filter(pk__in=request.data).update(is_delete=True,delete_time = datetime.now())
        return Response(status=status.HTTP_204_NO_CONTENT)